package com.ctg.behavior.calc.controller.interval;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.controller.retentions.RetentionsTMUInfo;
import com.ctg.behavior.common.CtgResult;
import com.ctg.behavior.common.interval.CtIntervalCalcRequest;
import com.ctg.behavior.common.interval.CtIntervalCalcResult;
import com.ctg.behavior.common.retentions.CtRetentionsCalcRequest;
import com.ctg.behavior.common.retentions.CtRetentionsCalcResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/interval")
public class IntervalController {

    @Value("${simulator.path}")
    private String simlutorPath;

    @PostMapping("/report")
    public CtIntervalCalcResult report(@RequestBody CtIntervalCalcRequest request) throws Exception {

        CtIntervalCalcResult result = new CtIntervalCalcResult();
        log.info("request", JSON.toJSONString(request));

        // 检查文件并读取文件
        File targetFile = new File("interval/report/interval.json");
        if (!targetFile.exists()) {
            ClassPathResource classPathResource = null;
            classPathResource = new ClassPathResource("interval/report/interval.json");
            InputStream inputStream = classPathResource.getInputStream();
            List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
            StringBuilder sbuilder = new StringBuilder();
            for (String line : lines) {
                sbuilder.append(line);
            }
            result = JSON.parseObject(sbuilder.toString(), CtIntervalCalcResult.class);
        } else {
            String resultText = FileUtils.readFileToString(targetFile, "UTF-8");
            result = JSON.parseObject(resultText.toString(), CtIntervalCalcResult.class);
        }

        log.info("result", JSON.toJSONString(result));

        return result;
    }

    @PostMapping("/reportTMU")
    public CtgResult report(@RequestBody RetentionsTMUInfo info) throws Exception {
        CtgResult result = new CtgResult();

        String filePath = simlutorPath + "/retentions/report";
        FileUtils.forceMkdir(new File(filePath));

        String targetName = this.builderTargetFileName(filePath, info.getRequest());
        String targetFilePath = filePath + targetName;

        File targetFile = new File(targetFilePath);
        FileUtils.writeStringToFile(targetFile, JSON.toJSONString(info.getResponse()), "UTF-8");

        //result.setMsg("report response body updload successfully!(test mode)");

        return result;
    }

    public String builderTargetFileName(String filePath, CtRetentionsCalcRequest request) {

        // 拼JSON文件名
        if (!request.getReq().getByFields().isEmpty()) {
            return "retentions/report/retentions.groupby.json";
        } else if (!request.getReq().getFirstEvent().getFilter().getConditions().isEmpty()) {
            return "retentions/report/retentions.nogroupby.json";
        } else {
            return "retentions/report/retentions.simple.json";
        }

    }

}
